﻿using OA.IRepository;
using OA.models.Root;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OA.Repository
{
    public class RepositoryBase<T>  where T : EntityBase, new()
    {
        DbContext context = DbContextFactory.CreateContext();

        public void Add(T obj)
        {
            context.Set<T>().Add(obj);
            
        }

        public void Delete(int id)
        {
            T obj=new T() { Id=id};

            context.Entry<T>(obj).State = EntityState.Deleted;
           
        }
         
        public T GetEntity(int id)
        {
            return context.Set<T>().Find(id);
        }

        public IQueryable<T> GetList(Expression<Func<T, bool>> where)
        {
            return context.Set<T>().Where(where);
        }

        public void Update(T obj)
        {
           context.Entry<T>(obj).State=EntityState.Modified;
            
        }
    }
}
